package main;

import utils.TextUtils;

import java.io.File;

public class InputBean {
    public static final String SPLITER = "&-&";
    private InputMode mode;
    private String path1;
    private String path2;
    private String path3;
    private String path4;

    public InputBean copy() {
        InputBean info = new InputBean();
        info.mode = this.mode;
        info.path1 = this.path1;
        info.path2 = this.path2;
        info.path3 = this.path3;
        info.path4 = this.path4;
        return info;
    }

    public void set(InputMode mode) {
        this.mode = mode;
    }

    public InputMode mode() {
        return mode;
    }

    public String path1() {
        return fixPath(path1);
    }

    public String path2() {
        return fixPath(path2);
    }

    public String path3() {
        return fixPath(path3);
    }

    public String path4() {
        return path4;
    }

    private InputBean fix() {
        switch (mode) {
            case Del -> {
                if (TextUtils.isEmpty(path2)) {//说明此时path1就是文件路径，不是文件夹
                    File file = new File(path1);
                    if (!file.isDirectory()) {
                        File parent = file.getParentFile();
                        path1 = parent.getAbsolutePath();
                        path2 = file.getName();
                    }
                }
            }
        }
        return this;
    }

    private String fixPath(String path) {
        if (!TextUtils.isEmpty(path)) {
            File file = new File(path);
            if (file.isDirectory() && !path.endsWith("/")) {
                return String.format("%s/", path);
            }
        }
        return path;
    }

    public static InputBean parse(String input) {
        String split = SPLITER;
        if (!TextUtils.isEmpty(input)) {
            InputBean result = new InputBean();
            if (input.contains(split)) {
                String[] splited = input.split(split);
                String tag = splited[0];
                InputMode mode = InputMode.parse(tag);
                if (mode != null) {
                    result.mode = mode;
                    if (splited.length > 1) {
                        result.path1 = splited[1];
                    }
                    if (splited.length > 2) {
                        result.path2 = splited[2];
                    }
                    if (splited.length > 3) {
                        result.path3 = splited[3];
                    }
                    if (splited.length > 4) {
                        result.path4 = splited[4];
                    }
                }
            } else if (TextUtils.equals(input, InputMode.Tip.tag())) {
                result.mode = InputMode.Tip;
            } else {//默认走CodeParser
                result.mode = InputMode.CodeParser;
                result.path1 = input;
            }
            return result.fix();
        }
        return null;
    }

    public static InputBean parse(String[] args) {
        if (args != null && args.length > 0) {
            String tag = args[0];
            InputBean result = new InputBean();
            InputMode mode = InputMode.parse(tag);
            if (mode != null) {
                result.mode = mode;
                String path1 = null;
                String path2 = null;
                String path3 = null;
                String path4 = null;
                if (args.length > 1) {
                    path1 = args[1];
                }
                if (args.length > 2) {
                    path2 = args[2];
                }
                if (args.length > 3) {
                    path3 = args[3];
                }
                if (args.length > 4) {
                    path4 = args[4];
                }
                result.path1 = path1;
                result.path2 = path2;
                result.path3 = path3;
                result.path4 = path4;
                return result.fix();
            }
        }
        return null;
    }
}
